<?php

namespace app\admin\model;

/**
 *  经销商采购订单
 */


use app\admin\model\OrderPayment;
use think\Model;

class WholesalOrder extends BaseModel
{
    protected $name = 'order';
    protected $attributes = [
        'status' => [
            10 => '待支付',     // 前端显示待支付
            20 => '审核中',  // 前端显示审核中
            21 => '审核通过',  // 前端显示待支付 审核通过 待支付
            70 => '已取消',  // 前端显示已取消
            100 => '已完成',  // 前端显示已完成
        ],
        'is_futures' => [
            0 => '现货',
            1 => '在途期货',
            2 => '大期货'
        ],
        'pay_status' => [
            1 => '未支付',
            2 => '待审核',
            3 => '已支付',
            4 => '部分支付'
        ],
        'pay_terms' => [
            0 => '全款',
            1 => '预付款'
        ],
        'pay_method' => [
            // 1 => 'Coinpal Pay',
            // 2 => 'Coinbase Pay',
            3 => 'Wire Transfer',
            10 => '现金'
        ],
        'delivery_method' => [
            0 => '自提',
            1 => '快递',
            2 => '仓内过货'
        ],
        'single_order_method' => [
            1 => '独立成单',
            2 => '合作成单',
            3 => '渠道成单'
        ],
        'contract_type' => [
            1 => '国内销售合同',
            2 => '国外销售合同'
        ],
        'contract_signing_status' => [
            0 => '未签署',
            1 => '已签署'
        ],
        'exceptionstatus' => [
            1 => '库存不足',
            2 => '未匹配到物流规则',
            3 => '商品/库存同步错误',
            4 => '批次库存可转数量不足'
        ],
        'transfer_payment_info' => [
            1 => 'HK-USD',
            2 => 'HK-EUR',
            3 => 'HK-HKD',
            4 => '豪利杰USD',
            5 => 'TRC20:USDT',
            6 => 'ERC20:USDT',
            7 => 'ERC20:USDC',
            8 => 'BEP20:BUSD(Binance Smart Chain)',
        ],
        'aftersales_status' => [
            1 => '退货中',
            4 => '已退货',
            8 => '退款中',
            16 => '已退款',
            32 => '已退货 已退款'
        ],
        'transfer_payment_info_detail' => [
            1 => [
                'detail_label' => 'HK-USD',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS （If you need 11-digit SWIFT Code: SZDBCNBSXXX）
Beneficiary's Account Number:
OSA15000112465560（USD）
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077 【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            2 => [
                'detail_label' => 'HK-EUR',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS （If you need 11-digit SWIFT Code: SZDBCNBSXXX）
Beneficiary's Account Number:
OSA15000112465366（EUR）
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            3 => [
                'detail_label' => 'HK-HKD',
                'detail_value' => "Beneficiary Bank: PING AN BANK CO., LTD.
Beneficiary's Bank Address: 5047, SHENNAN EAST ROAD, SHENZHEN 518001 P.R.CHINA
Beneficiary's Bank Swift: SZDBCNBS （If you need 11-digit SWIFT Code: SZDBCNBSXXX）
Beneficiary's Account Number:
OSA15000112465463（HKD）
Remittance Information: Purchase of goods
Beneficiary's Name: SUNRAYS INTERNATIONAL GROUP LIMITED
Beneficiary's Address: FLAT/RM 63 07/F WOON LEE COMMERCIAL BUILDING 7-9 AUSTIN AVE TSIM SHA TUSI KL HK,ZIP CODE:999077【Must be filled in on the Remittance Slip accurately】",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            4 => [
                'detail_label' => '豪利杰USD',
                'detail_value' => "Beneficiary Bank:   BANK OF COMMUNICATIONS SHANGHAI BRANCH YANGPU SUB-BRANCH
Address:  10TH FL. 99 ZHONG SHAN SOUTH RD. SHANGHAI 200010P.R. CHINA
Swift :  COMMCNSHSHI
Account Number: 310066690146300006278

Beneficiary:SHANGHAI COURAGE INTERNATIONAL TRADE CO. LTD
Address: R09, 4F, BLOCK 4NO.546 YUYUAN ROAD, 200040SHANGHAI CHINA ",
                'profile_label' => '豪利杰公司资料',
                'profile_value' => "PS.
Shanghai Courage International Trade Co.,Ltd is entrusted by Sunrays Info Tech Limited to receive the payment of this invoice."
            ],
            5 => [
                'detail_label' => 'TRC20:USDT',
                'detail_value' => "USDT Deposit Address(TRC20）：
TQppsbYu9fVRruyYWshirC614X8ooGXchb ",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            6 => [
                'detail_label' => 'ERC20:USDT',
                'detail_value' => "USDT Deposit Address(ERC20）：
0xCF5faeE1BE31f49106bE99d0eEed20866826De52",
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            7 => [
                'detail_label' => 'ERC20:USDC',
                'detail_value' => "USDC Deposit Address(ERC20）：
0xCF5faeE1BE31f49106bE99d0eEed20866826De52",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
            8 => [
                'detail_label' => 'BEP20:BUSD(Binance Smart Chain)',
                'detail_value' => "BUSD Deposit Address(BEP20）：
0xd2734dbcf245514d1c3987bb3648db4004d0c532",
                'profile_label' => '香港明曦公司资料',
                'profile_value' => "Sunrays International Group Limited
ROOM 63,7/F, WOON LEE COMMERCIAL BUILDING, 7-9 AUSTIN AVE, TSIM SHA TSUI, KOWLOON, HONG KONG, 999077"
            ],
        ]
    ];

    protected $enumCallbacks = [
        'currency' => 'getCurrency',
        'operator' => 'getOperatorEnum'
    ];

    public function getOperatorEnum()
    {
        return Operator::where('is_del', 0)->column('id as value,cname as label');
    }

    public function getCurrency()
    {
        return TransactionCurrency::where('is_del', 0)->column('id AS value,name as label');
    }

    public function getUserName()
    {
        return $this->belongsTo(Customer::class, 'user_id', 'id')->bind(['user_id_value' => 'cusname']);
    }

    public function getPayStatusValueAttr($value, $data)
    {
        return isset($this->attributes['pay_status'][$data['pay_status']]) ? $this->attributes['pay_status'][$data['pay_status']] : '';
    }

    public function getStatusValueAttr($value, $data)
    {
        return isset($this->attributes['status'][$data['status']]) ? $this->attributes['status'][$data['status']] : '';
    }

    public function getPayTermsValueAttr($value, $data)
    {
        return isset($this->attributes['pay_terms'][$data['pay_terms']]) ? $this->attributes['pay_terms'][$data['pay_terms']] : '';
    }

    public function getPayMethodIdValueAttr($value, $data)
    {
        return isset($this->attributes['pay_method'][$data['payment_method']]) ? $this->attributes['pay_method'][$data['payment_method']] : '';
    }

    public function productsList()
    {
        //待发货数量 = quantity -  lock_qty
        $field = [
            'id',
            'warehouse_id as wid',
            'is_futures',
            'arrival_date',
            'real_arrival_date',
            'original_price',
            'actual_price',
            '(pending_quantity - lock_qty) as quantity_to_be_shipped',
            'pending_quantity',
            'quantity',
            'product_id',
            'order_id',
            'free_shipping',
            'apply_rules',
            'price_selected_currency as actual_selling_price',
            'transaction_currency_id as actual_quoted_currency',
            'first_leg_logistics_fee',
            'exceptionstatus',
            'product_abbr'
        ];
        return $this->hasMany(WholesalOrderItem::class, 'order_id', 'id')->where('is_del', 0)->field($field)->with('product');
    }

    public function paymentList()
    {
        $field = [
            'id',
            'payment_method',
            'pay_account',
            'voucher_code',
            'transfer_voucher_id',
            'transfer_recepit_id',
            'pay_code',
            'pay_currency',
            'payment_url',
            'pay_time',
            'pay_amount',
            'reviewer',
            'status',
            'notes',
            'id',
            'order_id',
            'reviewer_time',
            'order_no',
            'order_type',
            'paid_amount',
            'fee_type',
            'transfer_type',
            'remark'
        ];
        return $this->hasMany(OrderPayment::class, 'order_id', 'id')->where('order_type', 'wholesal')->field($field);
    }

    public function orderList()
    {
        $field = ['wholesal_id', 'id', 'order_id', 'product_id'];
        return $this->hasMany(OrderItem::class, 'wholesal_id', 'id')->field($field)->with(['orders']);
    }

    public function logger()
    {
        $field = ['operate as title', 'content as info', 'create_time', 'entity_id'];
        return $this->hasMany(BusinessLog::class, 'entity_id', 'id')->where('business_module', 'wholesal_order')->field($field)->order('create_time', 'asc');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'user_id', 'id')->bind(['user_name' => 'cusname', 'user_email' => 'email']);
    }

    public function items()
    {
        return $this->hasMany(WholesalOrderItem::class, 'order_id', 'id');
    }

    public function itemList()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id')->where('is_del', 0)->with(['warehouse', 'product']);
    }

    public function product()
    {
        return $this->hasMany(Product::class, 'id', 'product_id');
    }



    public function getFieldEnum($field)
    {
        return $this->getEnum($field, [], 'label');
        //        if ('ALL' === strtoupper($field)) {
        //            return [
        //                'status' => transLabel($this->attributes['status']),
        //                'pay_status' => transLabel($this->attributes['pay_status']),
        //                'delivery_method' => transLabel($this->attributes['delivery_method']),
        //                'pay_method' => transLabel($this->attributes['pay_method'])
        //            ];
        //        } else {
        //            return [$field => $this->$field];
        //        }
    }
}
